package edu.sysu.cshare.util.gui;

import java.util.List;

import javax.swing.table.AbstractTableModel;

import edu.sysu.cshare.control.ClassManager;
import edu.sysu.cshare.control.HomeworkManager;
import edu.sysu.cshare.model.Course;
import edu.sysu.cshare.model.User;
import edu.sysu.cshare.util.HibernateSessionFactory;

public class TableModel_Course extends AbstractTableModel {
	private ClassManager classManager;
	private final String[] columnNames = { "id", "是否选择", "课程名称", "任课老师",
			"课程类型", "学分", "已选人数" };
	private List<Course> courses;

	private User user;

	public TableModel_Course(ClassManager classManager) {
		super();

		this.classManager = classManager;
		this.courses = classManager.getCourses();
		this.user = classManager.getActor();
	}

	@Override
	public Class<?> getColumnClass(int columnIndex) {

		return getValueAt(0, columnIndex).getClass();
	}

	@Override
	public int getColumnCount() {

		return columnNames.length;
	}

	@Override
	public String getColumnName(int column) {

		return columnNames[column];
	}

	@Override
	public int getRowCount() {

		return courses.size();
	}

	private void printDebugData() {
		int numRows = getRowCount();
		int numCols = getColumnCount();

		for (int i = 0; i < numRows; i++) {
			System.out.print("    row " + i + ":");
			for (int j = 0; j < numCols; j++) {
				System.out.print("  " + getValueAt(i, j));
			}
			System.out.println();
		}
		System.out.println("--------------------------");
	}

		/*@Override
		public boolean isCellEditable(int rowIndex, int columnIndex) {
			if (columnIndex == 1)
				return true;
			else
				return false;
		}

		@Override
		public void setValueAt(Object aValue, int rowIndex, int columnIndex) {

			if (columnIndex == 1) {
				super.setValueAt(aValue, rowIndex, columnIndex);

				fireTableCellUpdated(rowIndex, columnIndex);
				printDebugData();
			} else
				return;

		}*/
	
	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		Object value = null;
		switch (columnIndex) {
		case 0:
			value = courses.get(rowIndex).getId();
			break;
		case 1:
			value = user.getCourses().contains(courses.get(rowIndex));
			//value = new Boolean(courses.get(rowIndex).hasStudent(user));
			//value = HomeworkManager.isStuInCourse(user, courses.get(rowIndex));
			break;
		case 2:
			value = courses.get(rowIndex).getName();
			break;
		case 3:
			value = courses.get(rowIndex).getTeacher().getName();
			break;
		case 4:
			value = courses.get(rowIndex).getType();
			break;
		case 5:
			value = courses.get(rowIndex).getPoints();
			break;
		case 6:
			value = courses.get(rowIndex).getStudents().size();
			break;

		}
		return value;
	}

}
